<?php

class Member_model extends Model{
	//นำข้อมูลการสมัครสมาชิก ลงฐานข้อมูล
	function create_member(){
		//variable
		$firstname = $this->input->post('firstname');
		$lastname = $this->input->post('lastname');
		$nickname = $this->input->post('nickname');
		$gender = $this->input->post('gender');
		$birthdate = $this->input->post('birthdate');
		$address = $this->input->post('address');
		$province = $this->input->post('province');
		$email = $this->input->post('email');
		$username = $this->input->post('username');
		$password = $this->input->post('password');

		//insert into database
		$data = array(
			'firstname'=>$firstname,
			'lastname'=>$lastname,
			'nickname'=>$nickname,
			'gender'=>$gender,
			'birthdate'=>$birthdate,
			'address'=>$address,
			'province'=>$province,
			'email'=>$email,
			'username'=>$username,
			'password'=>$password
		);

		$insert = $this->db->insert('membership',$data);
		return $insert;
	}
	
	//validate binding database
	function validate(){
		$this->db->where('username',$this->input->post('username'));
		$this->db->where('password',$this->input->post('password'));
		$query = $this->db->get('membership');
		foreach($query->result() as $rows){
			$access_level = $rows->access_level;
			$member_id = $rows->member_id;
		}
		
		if($query->num_rows == 1){	//administrator level = 1
			$data = array(
				'username' => $this->input->post('username'),
				'is_logged_in' => true,
				'access_lev' => $access_level,
				'member_id' => $member_id
			);
			//set session.
			$this->session->set_userdata($data);
			return true;
		}
	}
	
	function upload_display($data){
		$file_name =  $data['raw_name']."_thumb".$data['file_ext'];
		
		$update_display = array('member_display' => $file_name);
		
		$member_id = $this->session->userdata('member_id');
		
		$this->db->where('member_id',$member_id);
		
		$this->db->update('membership',$update_display);
		
		return true;
	}
	
	function edit_password($data){
		$dataupdate = array(
			'password' => $data['new_password']
		);
		$this->db->where('member_id',$data['id']);
		$query = $this->db->update('membership',$dataupdate);
		
		return true;
	}
	
	function edit_userprofile(){
			//variable
			$firstname = $this->input->post('firstname');
			$lastname = $this->input->post('lastname');
			$nickname = $this->input->post('nickname');
			$gender = $this->input->post('gender');
			$birthdate = $this->input->post('birthdate');
			$address = $this->input->post('address');
			$province = $this->input->post('province');
			$email = $this->input->post('email');

			//insert into database
			$data = array(
				'firstname'=>$firstname,
				'lastname'=>$lastname,
				'nickname'=>$nickname,
				'gender'=>$gender,
				'birthdate'=>$birthdate,
				'address'=>$address,
				'province'=>$province,
				'email'=>$email,
			);
			
			$this->db->where('member_id',$this->session->userdata('member_id'));
			$query = $this->db->update('membership',$data);
			
			return true;
	}
}
/* End of file member_model.php */